package com.csk.zsk.compents.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.csk.zsk.commons.dao.impl.BaseHibernateDao;
import com.csk.zsk.compents.dao.VideoDao;
import com.csk.zsk.compents.entity.Video;

@Repository
public class VideoDaoImpl extends BaseHibernateDao<Video, String> implements VideoDao {

	private static final String QUERY_VIDEO_BY_TYPE = "from Video v where v.type = ?";
	private static final String QUERY_VIDEO_BY_YEAR = "from  Video v where v.tv_year = ? and v.type = ?";
	private static final String QUERY_VIDEO_BY_AREA = "from  Video v where v.area = ? and v.type = ?";
	private static final String QUERY_VIDEO_BY_YEAR_AND_AREA = "from  Video v where v.tv_year = ? and v.area = ? and v.type = ?";
	private static final String QUERY_VIDEO_BY_LESS_THAN_YEAR = "from  Video v where v.tv_year <= ? and v.type = ?";
	private static final String QUERY_VIDEO_BY_LESS_THAN_YEAR_AND_AREA = "from  Video v where v.tv_year <= ? and v.area = ? and v.type = ?";
	private static final String QUERY_VIDEO_BY_TVNAME = "from  Video v where v.tv_name like ? and v.type in ('4','5','6','7') order by v.type";
	private static final String QUERY_VIDEO_BY_TVNAME_AND_TYPE = "from Video v where v.tv_name like ? and v.type = ? order by v.type";
	private static final String QUERY_VIDEO_BY_IDLIST = "from Video v where v.id in (:ids)";

	@SuppressWarnings("unchecked")
	@Override
	public List<Video> getVideoByType(String type, Integer num) {
		return createQuery(QUERY_VIDEO_BY_TYPE, type).setMaxResults(num).list();
	}

	@Override
	public List<Video> getVideoByYearAndArea(Integer year, String area, String type) {
		return find(QUERY_VIDEO_BY_YEAR_AND_AREA, year, area, type);
	}

	@Override
	public List<Video> getVideoByLessThanYearAndArea(Integer year, String area, String type) {
		return find(QUERY_VIDEO_BY_LESS_THAN_YEAR_AND_AREA, year, area, type);
	}

	@Override
	public List<Video> getVideoByYear(Integer year, String type) {
		return find(QUERY_VIDEO_BY_YEAR, year, type);
	}

	@Override
	public List<Video> getVideoByArea(String area, String type) {
		return find(QUERY_VIDEO_BY_AREA, area, type);
	}

	@Override
	public List<Video> getVideoByLessThanYear(Integer year, String type) {
		return find(QUERY_VIDEO_BY_LESS_THAN_YEAR, year, type);
	}

	@Override
	public List<Video> searchVideoByTVName(String tvName) {
		return find(QUERY_VIDEO_BY_TVNAME, "%" + tvName + "%");
	}

	public List<Video> searchVideoByTVNameAndType(String tvName, String type) {
		return find(QUERY_VIDEO_BY_TVNAME_AND_TYPE, "%" + tvName + "%", type);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Video> getVideoByIdList(List<String> idList) {
		Query query = getSession().createQuery(QUERY_VIDEO_BY_IDLIST);
		if (idList != null && idList.size() > 0) {
			return query.setParameterList("ids", idList).list();
		} else {
			return null;
		}
	}

}
